<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="/css/common.css">
    <link rel="stylesheet" href="/css/login.css">
</head>

<body>
    <div class="nav">
        gobang
    </div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <button id="submit">注册</button>
            </div>
        </div>
    </div>

</body>
<script src="/js/jquery-3.7.1.min.js"></script>
<script>
    //元素绑定事件
    let usernameInput = document.querySelector("#username");
    let passwordInput = document.querySelector('#password');
    let submitButton = document.querySelector('#submit');
    //提交按钮绑定点击事件
    submitButton.onclick = function() {
        $.ajax({
            type: "post",
            url: "/register",
            data: {
                username: usernameInput.value,
                password: passwordInput.value

            },
            success: function(user) {
                //请求成功
                //判断用户是否存在

                // console.log(user);
                if (user && user.userName) {
                    alert('注册成功'),
                    //重定向到游戏大厅
                    location.assign('/login.html')

                } else {
                    alert('注册失败')

                }
            },
            error: function() {
                //请求失败
                alert('注册失败')

            }
        })
    }
</script>
</html>